const axios = require("axios");
const cheerio = require("cheerio");
async function fetchPage(url) {
  const { data } = await axios.get(url);
  return data;
}
function anyContent(content) {
  const $ = cheerio.load(content);
  return $(".grap")
    .text()
    .replace(/[\t\n]/g, "");
}
async function fetchWeb(url) {
  const { data } = await axios.get(url);
  const $ = cheerio.load(data);
  const result = []; // [ {title:'xxxx',list:[{title:"",content:""}]},  {title:'xxxx',list:[]},  {title:'xxxx',list:[]}]
  const elements = $(".juan, .okok").toArray();
  let current = null;
  for (const el of elements) {
    const $el = $(el);
    // .juan  ~   okok   okok    .juan  ~   okok   okok
    if ($el.hasClass("juan")) {
      if (current) {
        result.push(current);
      }
      const href = $el.find("a").attr("href");
      const title = $el.find("a").text();
      current = {
        title, // ?
        href,
        list: [],
      };
    } else if ($el.hasClass("okok") && current) {
      const href = $el.find("a").attr("href");
      const title = $el.find("a").text();

      const linkContent = await fetchPage(href);

      let content = anyContent(linkContent);

      current.list.push({ title, content });
    }
  }
  if (current) {
    result.push(current);
  }
  return result;
}

fetchWeb("https://lunyu.5000yan.com/").then((data) => {
  console.log(JSON.stringify(data, null, 2));
});
